<?php
namespace App\Models\Chain\Invite;


use App\Constants\CreditLogConstant;
use App\Models\Factory\InviteFactory;
use App\Models\Chain\AbstractHandler;
use App\Models\Factory\UserCreditFactory;
use App\Models\Factory\UserFactory;
use App\Models\Factory\UserInviteFactory;
use App\Models\Orm\AwUserIntegralLog;


class CreateCreditLogAction extends AbstractHandler
{
    private $params = array();
    protected $error = array('error' => '更新用户积分日志表失败', 'code' => 7011);


    public function __construct($params)
    {
        $this->params = $params;
    }


    public function handleRequest()
    {
        if ($this->updateCreditLog($this->params)) {
            return true;
        } else {
            return $this->error;
        }
    }


    //更新用户积分日志
    public static function updateCreditLog($params)
    {
        //通过手机号拿到邀请表中的id
        $invite_id = UserInviteFactory::getInviteUserId($params['userphone']);

        $user = User::where('id',$params['user_id'])->first();

        $action = AwUserIntegralLog::ACTION_INVITE;
        $type = AwUserIntegralLog::TYPE_ADD;
        $userIntegral = $user->getIntegral()->integral;
        $status =  AwUserIntegralLog::STATUS_ACCESS;

        return  UserCreditFactory::createCreditLogInvite($invite_id,$params['user_id'],$params['integral'],$action,$type,$status,$userIntegral);
    }
}